using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Store Byte
    /// </summary>
    public class Sb : MemoryAccessInstruction
    {
        public override int CycleCount
        {
            get { return 1; }
        }

        public Sb()
            : base("sb")
        {
        }

        /// <summary>
        /// Stores the contents of the least significant byte of register rD, into 
        /// the memory location that results from adding the contents of registers 
        /// rA and rB.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint ra = MicroblazeComponent.ReadRegister(SourceRegisterA);
            uint rb = MicroblazeComponent.ReadRegister(SourceRegisterB);
            uint rd = MicroblazeComponent.ReadRegister(DestinationRegister);

            WriteByteTransaction transaction = new WriteByteTransaction(MicroblazeComponent, ra + rb, rd);
            MicroblazeComponent.BeginDataTransaction(transaction);

            while (transaction.Status != Transaction.TransactionStatus.Complete)
                MicroblazeComponent.WaitOneCycle();

//            return MicroblazeComponent.DataMemorySpace.WriteByte(ra + rb, (byte)rd);
        }

        public override string ToString()
        {
            return string.Format("sb r{0}, r{1}, r{2}",
              DestinationRegister,
              SourceRegisterA,
              SourceRegisterB
            );
        }
    }
}
